package com.example.myapplication.Activity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.Utils.HttpUtils;
import com.example.myapplication.adapter.ItemListMainAdapter;
import com.example.myapplication.R;
import com.example.myapplication.TopBean;
import com.example.myapplication.top;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity  {
    String url = "http://v.juhe.cn/toutiao/index?type=%E5%9B%BD%E5%86%85&key=0af60a86bfa3575d53c1491d1be310ca";
    private ListView mainListview;
    String URl;
    List<top> list;
    List<TopBean.ResultBean.DataBean> beanList;
    ItemListMainAdapter adapter;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        loadNetDataAsyncTak tak=new loadNetDataAsyncTak();
        tak.execute(url);



    }

    private void addDataForList(List<TopBean.ResultBean.DataBean> beanList) {
        list = new ArrayList<>();
        for (int i = 0; i < beanList.size(); i++) {
            top top = new top(beanList.get(i).getTitle(), beanList.get(i).getThumbnail_pic_s(), beanList.get(i).getThumbnail_pic_s02()
                    , beanList.get(i).getThumbnail_pic_s03(), beanList.get(i).getDate(), beanList.get(i).getAuthor_name());
            list.add(top);
        }
        adapter = new ItemListMainAdapter(this, list);
        mainListview.setAdapter(adapter);
    }




    private void initView() {
        mainListview = (ListView) findViewById(R.id.main_listview);
        tv = (TextView) findViewById(R.id.tv);
    }



    //1.执行异步任务，传入的数据类型，2.表示下载速度，3.在后台执行耗时操作，返回主线的数据
    class loadNetDataAsyncTak extends AsyncTask<String, Void, String> {
        ProgressDialog dialog;
        public loadNetDataAsyncTak(){
            dialog=new ProgressDialog(MainActivity.this);
            dialog.setTitle("显示信息");
            dialog.setMessage("加载中");
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
            //开启子线程，执行异步请求之前，会调用的函数，通常执行初始化
        }

        @Override
        protected String doInBackground(String... strings) {
            //开启子线程，进行耗时操作。、，将获取到的数据，返回给主线程
            String json = HttpUtils.getJsonByNetwork(strings[0]);
            return json;
        }


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //回到主线程，将子线程传回来的数据，进行操作，更新UI
            //判断如果子线程返回的数据不为空，就显示在UI控件上
            if (!TextUtils.isEmpty(s)) {
                dialog.dismiss();
                tv.setText(s);
                Gson gson = new Gson();
                TopBean topBean = gson.fromJson(s, TopBean.class);
                beanList = topBean.getResult().getData();
                addDataForList(beanList);
            }
        }
    }
}
